package com.cy.member.parent;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.JWindow;

/**
 * 自己定义的一个JWindow类
 * @author CY
 *
 */
public class CYWindow extends JWindow {

	private static final long serialVersionUID = 1L;
	
	private Point loc = null;
	private Point tmp = null;
	private boolean isDragged = false;
	private JWindow jWindow = this;
    
	public CYWindow() {
		//设置默认属性
		this.setVisible(true);
	    removable();
	}

	/**
	 * 让窗体可移动
	 */
	private void removable() {
		//窗体移动，切换鼠标样式
		this.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent e) {
                isDragged = false;
             }
             public void mousePressed(java.awt.event.MouseEvent e) {
                tmp = new Point(e.getX(), e.getY());
                isDragged = true;
             }
         });
		//实现窗体移动
		this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent e) {
                if(isDragged) {
                    loc = new Point(jWindow.getLocation().x + e.getX() - tmp.x,
                    		jWindow.getLocation().y + e.getY() - tmp.y);
                    jWindow.setLocation(loc);
                } 
             }
         });
	}

	/**
	 * 让窗体居中
	 */
	public void locationCenter() {
		//窗体居中
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = this.getSize();
		this.setLocation((screenSize.width - frameSize.width) / 2,(screenSize.height - frameSize.height ) / 3);
	}
	
}
